package com.zhang.gulimall.order.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //todo 用RequestInterceptor实现，请求拦截器，在请求发出前拦截并添加请求头
        String uri = request.getRequestURI();
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean match = antPathMatcher.match("/order/order/getOrderStatus/**", uri);
        if (match) {
            return true;
        }

        Object loginUser = request.getSession().getAttribute("loginUser");
        if (loginUser != null) {
            return true;
        } else {
            response.sendRedirect("http://localhost:15000/login.html");
            return false;
        }

    }
}
